import tim from '../../tim';
import router from '../../router';
import http from '../../utils/http';
import testUserSig from '../../utils/testUserSig';

const user = {
  state: {
    isLogin: false,
    isLoginItemVisible: false,
    isMyProfileItemVisible: false,
    isIMSDKReady: false,
    currentUserProfile: {}
  },
  mutations: {
    toggleIsLoginItemVisible(state,isLoginItemVisible){
      state.isLoginItemVisible = typeof isLoginItemVisible === 'undefined' ? !state.isLoginItemVisible : isLoginItemVisible;
    },
    toggleIsMyProfileItemVisible(state,isMyProfileItemVisible){
      state.isMyProfileItemVisible = typeof isMyProfileItemVisible === 'undefined' ? !state.isMyProfileItemVisible : isMyProfileItemVisible;
    },
    toggleIsLogin(state,isLogin){
      state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin;
    },
    toggleIsSDKReady(state,isSDKReady){
      state.isIMSDKReady = typeof isSDKReady === 'undefined' ? !state.isIMSDKReady : isSDKReady;
    },
    updateCurrentUserProfile(state, userProfile) {
      state.currentUserProfile = userProfile;
    },
    reset(state) {
      Object.assign(state, {
        isLogin: false,
        isLoginItemVisible: false,
        isMyProfileItemVisible: false,
        isIMSDKReady: false,
        currentUserProfile: {}
      });
    }
  },
  actions: {
    login(context,userID){
      // 登录前初始化数据
      let sdkAppId = testUserSig.sdkAppId;
      let userSig = '';
      // 从服务端请求签名
      http.getIMSign(
        JSON.stringify({ 'userID': userID })
      ).then(respons => {
        if(respons) {
          userSig = respons;
        } else {
          userSig = testUserSig.users.find( user => userID === user.userID ).userSig;
        }
        context.dispatch('timLogin', { sdkAppId, userID, userSig });
      });
    },
    timLogin(context,options){
      const sdkAppId = options.sdkAppId;
      const userID = options.userID;
      const userSig = options.userSig;
      tim.login({
        userID,
        userSig
      })
      .then(() => {
        context.commit('toggleIsLogin', true);
        context.commit('startComputeCurrent');
        context.commit('updateUserInfo', { sdkAppId, userID, userSig });
        context.commit('showMessage', { message: '登录成功' , type: 'success' });
        if(router.currentRoute.path === '/avchatroom-item'){
          context.dispatch('joinGroup',{ groupID : 'avchatroom' });
          return; // 当前路径为音视频聊天室时，不做跳转和重置当前会话
        } 
        context.commit('resetCurrentConversation');
        router.push({ path: '/conversation-item' });
      })
      .catch(imError => {
        if (imError.code === 2000) {
          context.commit('showMessage', { message: '请检查是否正确填写了 SDKAPPID。', type: 'error' });
        } else {
          context.commit('showMessage', { message: imError.message, type: 'error' });
        }
      });
    },
    logout(context){
      tim.logout().then(() => {
        context.commit('reset');
        context.commit('stopComputeCurrent');
        context.commit('showMessage', { message: '登出成功', type: 'success' });
        router.push({path:'/home'});
      }).catch(imError => {
        context.commit('showMessage', { message: imError, type: 'warn' });
      });
    },
    getMyProfile(context){
      tim.getMyProfile().then(({ data }) => {
        context.commit('updateCurrentUserProfile', data);
      }).catch(error => {
        context.commit('showMessage', { message: error.message, type: 'error' });
      });
    },
    updateMyProfile(context,options){
      tim.updateMyProfile(options).then(({ data }) => {
        context.commit('updateCurrentUserProfile', data);
        context.commit('showMessage', { message: '修改个人资料成功', type: 'success' });
      }).catch(error => {
        context.commit('showMessage', { message: error.message, type: 'error' });
      });
    }
  }
}

export default user;
